
package com.csc440.team2.activity;

import java.io.File;
import java.util.List;


import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.csc440.team2.SmilConstants;
import com.csc440.team2.smil.SMILComponent;
import com.csc440.team2.smil.SMILMessage;
import com.csc440.team2.smil.SMILTextComponent;


public class SMILPlayer extends Activity
{
    private Handler mHandler = new Handler();


    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.player);

        LinearLayout layout = (LinearLayout) this.findViewById(R.id.playerLayout);
        layout.setOrientation(LinearLayout.VERTICAL);
        System.out.println("got layout");
        try
        {
            File sdRoot = Environment.getExternalStorageDirectory();

            if (sdRoot.canRead())
            {
                Bundle smilFile = getIntent().getExtras();
                SMILMessage value;
                if (smilFile != null)
                {
                    value = (SMILMessage) smilFile.getSerializable(SmilConstants.MESSAGE);

                    List<SMILComponent> componentList = value.getComponentList();

                    for (SMILComponent component : componentList)
                    {
                        switch (component.getType())
                        {
                            case SmilConstants.COMPONENT_TYPE_TEXT:
                                TextView textView = new TextView(this);
                                textView.setText(((SMILTextComponent) component).getText());

                                layout.addView(textView);
                                mHandler.postDelayed(new RemoveTask(textView), component.getDuration() * 1000);
                                break;
                        }

                    }
                }
            }
        }
        catch (Exception e)
        {
            Log.e("Exception", "error occurred while creating xml file", e);
        }
    }

    private class RemoveTask implements Runnable
    {
        private View view;


        public RemoveTask(View view)
        {
            this.view = view;
        }


        public void run()
        {
            view.setVisibility(View.INVISIBLE);
        }

    }
}
